Page({
  data: {
    listData: [
      { name: '头像', avatar: '/images/star_setting/Member_Tasks/individual.png' },
      { name: '会员相片' },
      { name: '手机号', phone: '18172442874' },
      { name: '姓名', username: '' },
      { name: '性别', gender: '' },
      { name: '生日', birthday: '' },
      { name: '入会时间' }
    ]
  },

  // 点击统一处理
  handleItemTap(e) {
    const index = e.currentTarget.dataset.index;
    const item = this.data.listData[index];

    switch (item.name) {
      case '头像':
        this.changeAvatar(index);
        break;
      case '手机号':
        this.editPhone(index);
        break;
      case '姓名':
        this.editName(index);
        break;
      case '性别':
        this.editGender(index);
        break;
      default:
        wx.showToast({ title: '点击了：' + item.name, icon: 'none' });
    }
  },

  // 生日选择
  onBirthdayChange(e) {
    const index = e.currentTarget.dataset.index;
    const list = this.data.listData;
    list[index].birthday = e.detail.value;
    this.setData({ listData: list });
  },

  // 修改性别
  editGender(index) {
    const list = this.data.listData;
    wx.showActionSheet({
      itemList: ['男', '女'],
      success: (res) => {
        const gender = res.tapIndex === 0 ? '男' : '女';
        list[index].gender = gender;
        this.setData({ listData: list });
      }
    });
  },

  // 修改姓名
  editName(index) {
    const list = this.data.listData;
    if (!list[index]) {
      wx.showToast({ title: '数据异常', icon: 'none' });
      return;
    }

    wx.showModal({
      title: '填写姓名',
      editable: true,
      placeholderText: '请输入姓名',
      success: (res) => {
        if (res.confirm && res.content) {
          const username = res.content.trim();
          if (!username) {
            wx.showToast({ title: '姓名不能为空', icon: 'none' });
            return;
          }
          list[index].username = username;
          this.setData({ listData: list });
        }
      }
    });
  },

  // 修改手机号
  editPhone(index) {
    wx.showModal({
      title: '填写手机号',
      editable: true,
      placeholderText: '请输入手机号',
      success: (res) => {
        if (res.confirm && res.content) {
          const phone = res.content.trim();
          const list = this.data.listData;
          list[index].phone = phone;
          this.setData({ listData: list });
        }
      }
    });
  },

  // 修改头像
  changeAvatar(index) {
    wx.showActionSheet({
      itemList: ['拍照', '从相册选择', '使用微信头像'],
      success: (res) => {
        if (res.tapIndex === 0 || res.tapIndex === 1) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: res.tapIndex === 0 ? ['camera'] : ['album'],
            success: (mediaRes) => {
              const tempFilePath = mediaRes.tempFiles[0].tempFilePath;
              this.updateAvatar(index, tempFilePath);
            }
          });
        } else if (res.tapIndex === 2) {
          wx.getUserProfile({
            desc: '用于设置微信头像',
            success: (userRes) => {
              const avatarUrl = userRes.userInfo.avatarUrl;
              this.updateAvatar(index, avatarUrl);
            }
          });
        }
      }
    });
  },

  updateAvatar(index, newAvatarUrl) {
    const list = this.data.listData;
    list[index].avatar = newAvatarUrl;
    this.setData({ listData: list });
  },

  // 生命周期（如需可继续实现）
  onLoad() {},
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
});
